package nl.project.registration;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import nl.project.data.Student;

public class Confirmation extends HttpServlet {
	
	/**
	 * If the user gets here, then they must have got here manually (without redirect from
	 * login page). So first check if the user has logged in. 
	 */
	@Override
	public void doGet(HttpServletRequest req, HttpServletResponse resp) 
			throws IOException {
		try {
			if (req.getSession().getAttribute("user") != null) {
				doPost(req, resp);
			} else {
				//user needs to login
				resp.sendRedirect("");
			}
		} catch (ServletException e) {
			//return something like: "Something went wrong! If problem stays, contact admin!"
		}
	}
	
	@Override
	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		HttpSession session = req.getSession();
		Student s = (Student) session.getAttribute("student");
		char classCode = s.getClassCode().toLowerCase().charAt(1);
		String day = "";
		// TODO dagen fixen uit DB oid!
		switch (classCode) {
			case 'h' : // HAVO
				day = "dinsdag";
				break;
			case 'v' : // VWO
				day = "woensdag";
				break;
			case 'b' : // VMBO
				day = "maandag";
				break;
			case 'm' : // MAVO?
				day = "maandag";
				break;
		}
		HashMap<String, String> data = putEnrollmentInRequest(req, day);
		
		PrintWriter out = resp.getWriter();
		
		out.println("<html>");
		out.println("<head><title>Bevestiging</title><link rel='stylesheet' type='text/css' href='/include/style.css' /></head>");
		out.println("<body>");
		RequestDispatcher rd = req.getRequestDispatcher ("/include/header.jsp");  
	    rd.include(req, resp);
		out.println("<h2>Bevestiging afspraak</h2>");
		out.println("<p>Weet u zeker dat u onderstaande afspraak wilt maken?</p>");
		out.println("<h3>Huidige afspraak: </h3>");
		out.println(" <table border = 2>");
		
		out.println("  <tr>");
		out.println("   <td>Naam</td>");
		out.println("   <td>"+s.getName()+"</td>");
		out.println("  </tr>");
		
		out.println("  <tr>");
		out.println("   <td>Dag</td>");
		out.println("   <td>"+day+"</td>");
		out.println("  </tr>");
		
		out.println("  <tr>");
		out.println("   <td>Gekozen vakken</td>");
		out.println("   <td>"+data.get("subjects")+"</td>");
		out.println("  </tr>");
		
		out.println("  <tr>");
		out.println("   <td>Voorkeur tijd</td>");
		out.println("   <td>"+data.get("prefTime")+"</td>");
		out.println("  </tr>");
		out.println(" </table>");
		
		out.println("<br />");
		
		out.println("<form action=\"/save_appointment\" method=\"POST\">");
		out.println(" <input type=\"hidden\" name='subjects' value=\""+data.get("subjects")+"\" />");
		out.println(" <input type=\"hidden\" name='days' value=\""+day+"\" />");
		out.println(" <input type=\"hidden\" name='pref' value=\""+data.get("prefTime")+"\" />");
		
		out.println(" <input type=\"submit\" value=\"Bevestig afspraak!\" />");
		out.println("</form>");
		
		out.println("</body></html>");
	}
	
	private HashMap<String, String> putEnrollmentInRequest(HttpServletRequest req, String day) {
		HashMap<String, String> data = new HashMap<String, String>();
		StringBuilder sbSubjects = new StringBuilder();
		
		if (day == "maandag") {
			req.setAttribute("day_no_pref", false);
			req.setAttribute("day0", true);
			req.setAttribute("day1", false);
			req.setAttribute("day2", false);
		} else if (day == "dinsdag") {
			req.setAttribute("day_no_pref", false);
			req.setAttribute("day0", false);
			req.setAttribute("day1", true);
			req.setAttribute("day2", false);
		} else {
			req.setAttribute("day_no_pref", false);
			req.setAttribute("day0", false);
			req.setAttribute("day1", false);
			req.setAttribute("day2", true);
		}
		
		data.put("days", day);
		//looping through the subjects
		for (int i = 0; i < 3; i++) {
			if (!req.getParameter("subject"+i).equals("---------------------")) {
				req.setAttribute("subject"+i, req.getParameter("subject"+i));
				System.out.println(req.getParameter("subject" + i));
				sbSubjects.append(req.getParameter("subject"+i) + "; ");
			}
		}
		data.put("subjects", sbSubjects.toString());
		req.setAttribute("time", req.getParameter("time"));
		if (req.getParameter("time").equals("early")) {
			data.put("prefTime", "Vroeg in de avond");
		} else if (req.getParameter("time").equals("later")) {
			data.put("prefTime", "Laat in de avond");
		} else {
			data.put("prefTime", "Geen voorkeur");
		}
		return data;
	}
}